package j19.exception;

import java.util.Scanner;

public class MenuUtil {
	/**
	 * eg.	青岛是个好地方	title
	 * 		1.沿海城市,气候宜人	items	2个
	 * 		2.经济发达
	 * 		请选择:			//从0开始,别搞混了
	 * 
	 * @param title		标题
	 * @param items		menu下得子标题
	 * @return
	 */
	public static int showMenuBasic(String title, String[] items) {
		Scanner input = new Scanner(System.in);
		System.out.println(title);
		for (int i = 0; i < items.length; i++) {
			System.out.println((i + 1) + "." + items[i]);
		}
		System.out.print("请选择：");
		return input.nextInt()-1;	//方便按照下标查找
	}

	public static int showMenu(String title, String[] items) {
		Scanner input = new Scanner(System.in);
		System.out.println(getMenu(title, items, ""));
		return input.nextInt();
	}

	public static String getMenu(String title, String[] items, String sign) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(title + sign + "(" + sign);
		for (int i = 0; i < items.length; i++) {
			sbf.append((i + 1) + "." + items[i] + "  " + sign);
		}
		sbf.append(")" + sign + "请选择：");
		return sbf.toString();
	}

	/**
	 * 使用了正则表达式,输入的按照字符串获得数据
	 * @param title
	 * @param items
	 * @return
	 */
	public static int showMenu3(String title, String[] items) {
		String tishi = getMenu(title, items, "");
		
		//一边输出提示, 一边获得想要的String 信息
		String id = InputUtil.getStringCheck(tishi, "[1-" + items.length
				+ "]{1}", "菜单序号错误！");
		return Integer.valueOf(id);
	}

	/**
	 * 使用了正则表达式,输入的按照字符串获得数据
	 * @param title
	 * @param items
	 * @return
	 */
	public static int showMenu4(String title, String[] items) {
		String tishi = getMenu(title, items, "\n");//获得所有提示信息
		
		/**
		 * 这里开始获得
		 */
		String id = InputUtil.getStringCheck(tishi, "[1-" + items.length
				+ "]{1}", "菜单序号错误！");
		return Integer.valueOf(id);
	}
}
